//
//  MessageNextViewController.m
//  ZPIOSEducation
//
//  Created by 董伟1 on 17/1/18.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "MessageNextViewController.h"
#import "MessageNextCell.h"
#import "HomeWorkDetailViewController.h"
#import "MJRefresh.h"
#import "ZPBaseTabBarController.h"

@interface MessageNextViewController () <UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray  *dataArray;
}

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;

@end

@implementation MessageNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"消息";
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    
    dataArray = [[NSMutableArray  alloc] init];
    
    __weak MessageNextViewController *weakSelf = self;
    [self.mainTableView  addLegendHeaderWithRefreshingBlock:^{
        [weakSelf  requestData];
    }];
    
    [self  requestData];
    [self  sendUpdateMsgRequest];
    
    ZPBaseTabBarController  *tabbarController = (ZPBaseTabBarController  *)[UIApplication  sharedApplication].keyWindow.rootViewController;
    NSArray *items = tabbarController.tabBar.items;
    if (items.count > 2) {
        RDVTabBarItem *thirdItem = [items  objectAtIndex:2];
        thirdItem.badgeValue = @"";
    }
}

#pragma mark - 网络请求
#pragma mark 获取某个类别的消息列表
- (void)requestData
{
    [self  showHudithString:@"加载中..."];
    NSDictionary  *paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"memberId"],@"teacherId", nil];
    [[ZPNetworking  sharedZPNetworking] POST:APIPPT@"teacher/getMessageList.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nonnull responseObject) {
        NSLog(@"======%@",responseObject);
        [self  hiddenHud];
        if (self.mainTableView.header.isRefreshing) {
            [self.mainTableView.header  endRefreshing];
        }
        if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
            [dataArray  removeAllObjects];
            if ([[responseObject  objectForKey:@"result"] isKindOfClass:[NSArray  class]]) {
                [dataArray  addObjectsFromArray:[responseObject  objectForKey:@"result"]];
            }
            [self.mainTableView  reloadData];
            
            if (dataArray.count == 0) {
                [self  showNoDataViewWithTitle:@"还没有消息哦~"];
            }else {
                [self  hiddenNoDataView];
            }
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self  hiddenHud];
        if (self.mainTableView.header.isRefreshing) {
            [self.mainTableView.header  endRefreshing];
        }
    }];
}

#pragma mark 更新已读
- (void)sendUpdateMsgRequest
{
    NSDictionary  *paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"memberId"] ,@"teacherId", nil];
    [[ZPNetworking  sharedZPNetworking] POST:APIPPT@"teacher/updateMessage.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nonnull responseObject) {
        NSLog(@"++++++++++%@",responseObject);
        if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
            
        }
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - UITableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArray  count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString  *content = [NSString  stringWithFormat:@"%@",[[dataArray  objectAtIndex:indexPath.row]  objectForKey:@"content"]];
    if (content.length > 0) {
        CGSize  size = [content  calculateSize:CGSizeMake(SCREEN_WIDTH - 16 - 16 - 6, 2000) font:[UIFont  systemFontOfSize:12.]];
        return 106 + size.height;
    }else {
        return 121.;
    }
    
}

- (UITableViewCell  *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MessageNextCell";
    MessageNextCell  *cell = (MessageNextCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MessageNextCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSInteger  row = [indexPath  row];
    NSDictionary  *tempDic = [dataArray  objectAtIndex:row];
    
    cell.timeLab.text = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"createTime"]];
    NSInteger  type = [[tempDic  objectForKey:@"type"] integerValue];
    switch (type) {
        case 2:
            cell.titleLab.text = @"转班提醒";
            break;
        case 3:
            cell.titleLab.text = @"调课提醒";
            break;
        case 4:
            cell.titleLab.text = @"退班提醒";
            break;
            
        default:
            cell.titleLab.text = @"";
            break;
    }
    cell.detailLab.text = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"content"]];
    
    return cell;
    
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    NSDictionary  *tempDic = [dataArray  objectAtIndex:indexPath.row];
//    NSString  *type = [tempDic  objectForKey:@"type"];
//    NSString  *homeworkId = [tempDic  objectForKey:@"homeworkId"];
//    if (type && [type  integerValue] == 1 && homeworkId && ![homeworkId  isEqual:[NSNull  null]]) {
//        HomeWorkDetailViewController  *viewController = [[HomeWorkDetailViewController  alloc] initWithNibName:@"HomeWorkDetailViewController" bundle:nil];
//        viewController.idStr = [NSString  stringWithFormat:@"%@",homeworkId];
//        viewController.typeStr = @"3";
//        [self.navigationController  pushViewController:viewController animated:YES];
//    }
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
